import Cookies from "js-cookie"

const TokenKey = "mes_token" //srm-token
const UsernameKey = "mes_name"
const EnternameKey = "mes_enter_name"
const UsernameIdKey = 'mes_id'
const CheckKey = 'mes_check'
const PasswordKey = 'mes_password'
const EnterpriseCodeKey = 'mes_enterprise_code'
const NameKey = 'mes_user_name'
const LogoId = 'mes_logo_id'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token, check) {
  if (check) {
    return Cookies.set(TokenKey, token, { expires: 14 })
  } else {
    return Cookies.set(TokenKey, token)
  }
}

export function removeToken(check) {
  if (check) {
    return Cookies.remove(TokenKey);
  } else {
    return Cookies.remove(TokenKey)
  }
  //console.log(document.domain)
  
}
/**
 * 
 * 保持登录判断条件
 */
export function getCheck() {
  return Cookies.get(CheckKey)
}

export function setCheck(check) {
  // return Cookies.set(CheckKey, check)
  if (check) {
    return Cookies.set(CheckKey, check, { expires: 14 })
  } else {
    return Cookies.set(CheckKey, check)
  }
}

export function removeCheck(check) {
  if (check) {
    return Cookies.remove(CheckKey);
  } else {
    return Cookies.remove(CheckKey)
  }
}

//登录用户
export function getUserName() {
  return Cookies.get(UsernameKey)
}

export function setUserName(name, check) {
  // return Cookies.set(UsernameKey, name)
  if (check) {
    return Cookies.set(UsernameKey, name, { expires: 14 })
  } else {
    return Cookies.set(UsernameKey, name)
  }
}

export function removeUserName(check) {
  // return Cookies.remove(UsernameKey)
  if (!check) {
    return Cookies.remove(UsernameKey)
  }
}

//登录用户id
export function getUserNameId() {
  return Cookies.get(UsernameIdKey)
}

export function setUserNameId(id, check) {
  // return Cookies.set(UsernameIdKey, id)
  if (check) {
    return Cookies.set(UsernameIdKey, id, { expires: 14 })
  } else {
    return Cookies.set(UsernameIdKey, id)
  }
}

export function removeUserNameId(check) {
  // return Cookies.remove(UsernameIdKey)
  if (check) {
    return Cookies.remove(UsernameIdKey);
  } else {
    return Cookies.remove(UsernameIdKey)
  }
  
}

//企业名称
export function getEnterName() {
  return Cookies.get(EnternameKey)
}

export function setEnterName(enterName, check) {
  // return Cookies.set(EnternameKey, enterName)
  if (check) {
    return Cookies.set(EnternameKey, enterName, { expires: 14 })
  } else {
    return Cookies.set(EnternameKey, enterName)
  }
}
//企业logo id保存
export function getLogoId() {
  return Cookies.get(LogoId)
}

export function setLogoId(id,check) {
  if (check) {
    return Cookies.set(LogoId, id, { expires: 14 })
  } else {
    return Cookies.set(LogoId, id,)
  }
}
export function removeLogoId(check) {
  if (!check) {
    return Cookies.remove(LogoId)
  }
}

//用户密码
export function getPassword() {
  return Cookies.get(PasswordKey)
}

export function setPassword(password, check) {
  if (check) {
    return Cookies.set(PasswordKey, password, { expires: 14 })
  } else {
    return Cookies.set(PasswordKey, password)
  }
}

export function removePasswordKey(check) {
  if (!check) {
    return Cookies.remove(PasswordKey)
  }
}

//账号名
export function getNameKey() {
  return Cookies.get(NameKey)
}

export function setNameKey(username, check) {
  if (check) {
    return Cookies.set(NameKey, username, { expires: 14 })
  } else {
    return Cookies.set(NameKey, username)
  }
}

export function removeNameKey(check) {
  if (!check) {
    return Cookies.remove(NameKey)
  }
}

//企业编码
export function getEnterpriseCode() {
  return Cookies.get(EnterpriseCodeKey)
}

export function setEnterpriseCode(enterpriseCode, check) {
  if (check) {
    return Cookies.set(EnterpriseCodeKey, enterpriseCode, { expires: 14 })
  } else {
    return Cookies.set(EnterpriseCodeKey, enterpriseCode)
  }
}

export function removeEnterpriseCode(check) {
  if (!check) {
    return Cookies.remove(EnterpriseCodeKey)
  }
}

export function removeEnterName(check) {
  // return Cookies.remove(UsernameKey)
  if (check) {
    return Cookies.remove(EnternameKey);
  } else {
    return Cookies.remove(EnternameKey)
  }
}

export function handelData(val,data) {
  val.map(x => {
    if(x.children) {
      if(x.authority && (x.authority != 'dashboard')) {
        if(!data.includes(x.authority)) {
          x.hidden = true
        }
      }
      handelData(x.children,data)
    }else {
      if(x.path == '/login') {
        x.hidden = true
      }
      if(x.authority) {
        if(!data.includes(x.authority)) {
          x.hidden = true
        }
      }
    }
  })
  return val
}


